package cc.ding.dao.Impl;

import cc.ding.dao.BaseDao;
import cc.ding.dao.PunchClockDao;

import java.sql.ResultSet;
import java.sql.SQLException;

public class PunchClockDaoImpl extends BaseDao implements PunchClockDao {
    @Override
    public int todayPunching(Integer empId) {
        String sql = "SELECT count(*) count FROM attendance WHERE employeeId = ? AND date = CURDATE()";
        ResultSet resultSet = executeQuery(sql, empId);
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    return resultSet.getInt("count");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }

        return -1;
    }

    @Override
    public int createPunching(Integer empId) {
        String sql = "INSERT into attendance(employeeId,date) VALUES (?,CURDATE())";
        return executeUpdate(sql,empId);
    }

    @Override
    public int updateStartTime(Integer empId) {
        String sql = "UPDATE attendance SET startTime = CURTIME() WHERE employeeId = ? AND date = CURDATE()";
        return executeUpdate(sql,empId);
    }

    @Override
    public int updateEndTime(Integer empId) {
        String sql = "UPDATE attendance SET EndTime = CURTIME() WHERE employeeId = ? AND date = CURDATE()";
        return executeUpdate(sql,empId);
    }
}
